package com.kyzh.core.dialog

import android.app.Activity
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AlertDialog
import com.gushenge.core.requests.UserRequest
import com.kyzh.core.R
import com.kyzh.core.pager.login.LoginActivity
import com.kyzh.core.databinding.DialogSignResignBinding
import com.kyzh.core.utils.loadImage
import com.kyzh.core.utils.startActivity


private var signResignDialog:AlertDialog?=null

fun Activity.showSignResignDialog(date:String,huiyuan:String,sy:String,listener:()->Unit){
    signResignDialog = AlertDialog.Builder(this, R.style.kyzhGuestLoginDialog)
            .setView(getView(date,huiyuan,sy,listener))
            .create()

    signResignDialog?.show()
}
private fun Activity.getView(date:String,huiyuan:String,sy:String,listener:()->Unit):View{
    val binding = DialogSignResignBinding.inflate(LayoutInflater.from(this)).also {
        UserRequest.userCenter {
            if(this == null ){
                startActivity<LoginActivity>()
            }else{
                it.ivHead.loadImage(this.user.face)
                it.tvName.text = this.user.user_name
            }
        }
        it.tvSy.text = String.format(getString(R.string.daysRemain),huiyuan)
        it.tvNum.text = String.format(getString(R.string.reissueRemain),sy)
        val content = String.format(getString(R.string.reissueDesc),"<font color='#F87706'>${date}</font>")
        it.tv2.text = Html.fromHtml(content)
        it.tvSign.setOnClickListener {
                listener()
                signResignDialog?.dismiss()

        }
        it.cancel.setOnClickListener {
            signResignDialog?.dismiss()
        }
    }
    return binding.root
}